package com.sunday.aomi.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.utils.PixUtils;
import com.sunday.aomi.R;
import com.sunday.aomi.ui.product.ProductDetailActivity;
import com.sunday.member.entity.product.Product;

import java.math.RoundingMode;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by 刘涛 on 2017/5/25.
 */

public class ProductDetailRecAdapter extends RecyclerView.Adapter {

    private Context mContext;
    private List<Product> dataSet;

    private final static int TYPE_HEAD = 0;
    private final static int TYPE_LIST = 1;

    public ProductDetailRecAdapter(Context context, List<Product> datas) {
        dataSet = datas;
        mContext = context;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_HEAD:
                View view = LayoutInflater.from(mContext).inflate(R.layout.pro_detail_list_head, null);
                RecyclerView.LayoutParams layoutParams=new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, PixUtils.dip2px(mContext,45));
                view.setLayoutParams(layoutParams);
                return new HeadHolder(view);
            case TYPE_LIST:
                view = LayoutInflater.from(mContext).inflate(R.layout.product_list_grid_item, null);
                return new ListHolder(view);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position==0){
            HeadHolder headHolder= (HeadHolder) holder;
            if (dataSet.size()==0){
                headHolder.headLayout.setVisibility(View.GONE);}
            else {
                headHolder.headLayout.setVisibility(View.VISIBLE);
            }
        }else {
            ListHolder listHolder= (ListHolder) holder;
            Product item=dataSet.get(position-1);
            if (!TextUtils.isEmpty(item.getImage())){
                Picasso.with(mContext)
                        .load(item.getImage())
                        .error(R.mipmap.ic_default)
                        .resize(200,200)
                        .centerInside()
                        .into(listHolder.productImg);
            }
            listHolder.productPrice.setText(String.format("￥%s",item.getPrice().setScale(2, RoundingMode.HALF_UP)));
            listHolder.productOldPrice.setText(String.format("￥%s",item.getMarketPrice().setScale(2,RoundingMode.HALF_UP)));
            listHolder.productOldPrice.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
            listHolder.productTitle.setText(item.getName());
            listHolder.productLayout.setTag(item);
        }
    }

    @Override
    public int getItemCount() {
        return dataSet.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        } else {
            return TYPE_LIST;
        }
    }

    class HeadHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.header_layout)
        LinearLayout headLayout;
        public HeadHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    class ListHolder extends RecyclerView.ViewHolder implements View.OnClickListener{

        @Bind(R.id.product_img)
        ImageView productImg;
        @Bind(R.id.product_price)
        TextView productPrice;
        @Bind(R.id.product_old_price)
        TextView productOldPrice;
        @Bind(R.id.product_title)
        TextView productTitle;
        @Bind(R.id.product_layout)
        RelativeLayout productLayout;
        public ListHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
            productLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Product item= (Product) v.getTag();
            Intent intent=new Intent(mContext, ProductDetailActivity.class);
            intent.putExtra("productId",item.getId());
            mContext.startActivity(intent);
        }
    }
}
